package com.example.demo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import java.util.Date;

@Service
public class EmailService {
    @Autowired
    private JavaMailSenderImpl javaMailSender;//注入邮件工具类
    @Autowired
    private TemplateEngine templateEngine;
    private String s ="{\"Email\":\"WENDY.CHUA@ASPEED.COM.SG\",\"IsAgreePrivacy\":\"false\",\"IsSubscriber\":\"false\"}";
    public void sendMimeMail() throws MessagingException {
        MimeMessageHelper messageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);//true表示支持复杂类型
        messageHelper.setFrom("zhangxianwen2020@163.com");//邮件发信人
        messageHelper.setTo("978880162@qq.com");//邮件收信人
        messageHelper.setSubject("通知信息");//邮件主题
        Context context = new Context();// 设置模板中的变量
        context.setVariable("us", "000001");
        String process = templateEngine.process("emailf.html", context);
        messageHelper.setText(process, true);//邮件内容
        messageHelper.setText("c111111111");//邮件内容
        messageHelper.setSentDate(new Date());
        javaMailSender.send(messageHelper.getMimeMessage());//正式发送邮件
    }

}
